package com.cloudeasy.post.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudeasy.post.dao.mapper.PostLabelMapper;
import com.cloudeasy.post.po.PostLabel;
import com.cloudeasy.post.service.expand.AmqpService;
import com.cloudeasy.post.util.IPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class PostLabelService extends ServiceImpl<PostLabelMapper, PostLabel> {

    private final AmqpService amqpService;

    @Autowired
    public PostLabelService(AmqpService amqpService) {
        this.amqpService = amqpService;
    }

    /**
     * 根据帖子Id查找
     *
     * @param postId 帖子Id
     * @return {@link List<PostLabel>}
     */
    public List<PostLabel> getByPostId(String postId) {
        return this.list(wrapper(Collections.singletonList(postId), null));
    }

    /**
     * 根据帖子Id列表查找
     * @param postIds 帖子Id列表
     * @return {@link List<PostLabel>}
     */
    public List<PostLabel> getByPostIds(List<String> postIds) {
        return this.list(wrapper(postIds, null));
    }

    /**
     * 根据标签Id查找
     *
     * @param labelId 标签Id
     * @return List<PostLabel>
     */
    public List<PostLabel> getByLabelId(String labelId) {
        return this.list(wrapper(null, Collections.singletonList(labelId)));
    }

    /**
     * 查找帖子标签记录
     *
     * @param postIds  帖子Id列表
     * @param labelIds 标签Id列表
     * @return List<PostLabel>
     */
    public List<PostLabel> list(List<String> postIds, List<String> labelIds) {
        return this.list(wrapper(postIds, labelIds));
    }

    /**
     * 构造Wrapper
     *
     * @param postIds  帖子Id列表
     * @param labelIds 标签Id列表
     * @return {@link QueryWrapper<PostLabel>}
     */
    public QueryWrapper<PostLabel> wrapper(List<String> postIds, List<String> labelIds) {
        QueryWrapper<PostLabel> queryWrapper = new QueryWrapper<>();
        if (postIds != null && postIds.size() > 0) {
            queryWrapper.in("post_id", postIds);
        }
        if (labelIds != null && labelIds.size() > 0) {
            queryWrapper.in("label_id", labelIds);
        }
        return queryWrapper;
    }

    /**
     * 给帖子更新标签
     *
     * @param postId   帖子Id
     * @param labelIds 标签Id列表
     * @return {@link Boolean}
     */
    public Boolean update(String postId, List<String> labelIds) {
        List<String> rowLabelIds = this.getByPostId(postId).stream().map(PostLabel::getLabelId).distinct().collect(Collectors.toList());
        this.removeByPostIdAndLabelIds(postId, rowLabelIds);
        amqpService.sendAmqp(null, rowLabelIds, -1);
        amqpService.sendAmqp(null, labelIds, 1);
        List<PostLabel> postLabels = labelIds.stream().map(labelId -> PostLabel.builder().postId(postId).labelId(labelId).build())
                .collect(Collectors.toList());
        return this.saveBatch(postLabels);
    }

    /**
     * 根据帖子Id和标签Id列表删除记录
     *
     * @param postId   帖子Id
     * @param labelIds 标签Id列表
     * @return {@link Boolean}
     */
    public Boolean removeByPostIdAndLabelIds(String postId, List<String> labelIds) {
        if (labelIds.size() == 0) {
            return true;
        }
        QueryWrapper<PostLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id", postId)
                .in("label_id", labelIds);
        return this.remove(queryWrapper);
    }

    /**
     * 根据标签Id列表获取记录
     *
     * @param labelIds 标签Id列表
     * @return {@link List<PostLabel>}
     */
    public List<PostLabel> getByLabelIds(List<String> labelIds) {
        QueryWrapper<PostLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("label_id", labelIds);
        return this.list(queryWrapper);
    }
}
